<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>OSSJK 2.0</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<meta http-equiv="Cache-Control" content="no-siteapp">
<link rel="stylesheet" href="../../../static/layui/css/layui.css" media="all" />
<link rel="stylesheet" href="../../../static/css/public.css" media="all" />
<style>
.detailTable_text {
	font-weight: bold;
}
</style>
</head>
<body class="childrenBody">
	<form class="layui-form" lay-filter="formTest" id="form">
		<!-- 基本信息模块 -->
		<fieldset class="layui-elem-field layui-field-title site-title">
			<legend>
				<div>分配项目</div>
			</legend>
		</fieldset>
		<table class="layui-table mag0" id="subreimburse" lay-filter="subreimburse">
			<thead>
				<tr>
					<th></th>
					<th>跟进人</th>
					<th>分配率(100%)</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td colspan="6">
						<div style="text-align: center">
							<a class="layui-btn layui-btn-xs" id="subreimburse-add">添加</a>
						</div>
					</td>
				</tr>
			</tbody>
		</table>
		<div class="magt10 layui-right">
			<div class="layui-input-block">
				<button type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="submit">提交</button>
				<button type="button" class="layui-btn layui-btn-sm layui-btn-primary close-btn">取消</button>
			</div>
		</div>
	</form>
	<script type="text/javascript" src="../../../static/js/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="../../../static/layui/layui.js"></script>
	<script type="text/javascript" src="../../../static/js/index.js"></script>
	<script type="text/javascript" src="../../../static/custom/js/constant.js"></script>
	<script type="text/javascript" src="../../../static/custom/js/common.js"></script>
	<script type="text/javascript">
		layui.config({
			base : '../../../static/plugin/layui_extends/'
		}).extend({
			selectN : 'selectN'
		}).use([ 'selectN', 'form', 'laydate', 'layer' ], function() {
			var form = layui.form;
			var laydate = layui.laydate;
			var selectN = layui.selectN;
			var layer = parent.layer === undefined ? layui.layer : top.layer, $ = layui.jquery;
			
			var getEmployeeApi = "../../../getEmployee";
			var assignmentApi = "../../../advisory/customer/assignment";
			var assignmentMethod = "put";
			var url = assignmentApi;
			var method = assignmentMethod;
			
			var employeeList = null;
			
			//添加子项目
			$("#subreimburse-add").click(function() {
				
				var ids = id.split(',');
				var idslg = ids.length;
				var length = $(".sremarks").length;
				
				if((idslg/2)>length){
					$("#subreimburse-add").parent().parent().parent().before(subreimburseHtml(employeeList));
					length = $(".sremarks").length;
					var lv = 100/length;
					if(length>1){
						for(var i in $(".sremarks")){
							$(".sremarks")[i].value=lv.toFixed(2);
						}
					}
					form.render(); // 更新全部
					//显示删除按钮
					$(".deleteitem-btn").show();
					setText();
				}else{
					layer.msg("跟进人数量不得超过客户数量");
				}
			});

			//拼接子项目字段
			function subreimburseHtml(obj) {
				var str = null;
				str += '<tr class="subreimburse">                                                                                                   ';
				str += '	<td class="detailTable_text subreimburse-text">分配' + ($(".subreimburse").length+1) + '</td>                                                                               ';
				str += '	<td>                                                                                                                                      ';
				str += '		<select name="eid" class="sisreceipt" lay-verify="required">                                                         ';
				str += '			<option value="">选择分配人</option>                                                                                    ';
				for ( var i in obj) {
				str += '			<option value="'+obj[i].id+'">'+obj[i].rname+'</option>                                                                                               ';
				}
				str += '		</select>                                                                                                                         ';
				str += '	</td>                                                                                                                                     ';
				str += '	<td><input type="text" class="layui-input sremarks" lay-verify="required" name="lv" class="samount" value="100"></td>         ';
				str += '	<td><a class="layui-btn layui-btn-xs layui-btn-danger deleteitem-btn">删除</a></td>';
				str += '</tr>';
				return str;	
			}
			$("tbody").on("click", ".deleteitem-btn", function() {
				$(this).parent().parent().remove();
				var length = $(".sremarks").length;
				if(length>1){
					var lv = 100/length;
					for(var i in $(".sremarks")){
						$(".sremarks")[i].value=lv.toFixed(2);
					}
				}else{
					for(var i in $(".sremarks")){
						$(".sremarks")[i].value=100;
					}
				}
				if ($(".subreimburse").length <= 1) {
					$(".deleteitem-btn").hide();
				}
				setText();
			});
			function setText() {
				for (var i = 1; i <= $(".subreimburse").length; i++) {
					$(".subreimburse-text").eq(i - 1).text("分配项目" + i);
				}

			}
			//回显表单
			var id = getUrlParam("id");
			var type = getUrlParam("type");
			
			
			loadAjax( getEmployeeApi, "get", null, function(response) {
				baseCallBack(response, function(response) {
					employeeList = response.data;
					//客户id
					var ids = id.split(',');
					for(var i in ids){
						$("#subreimburse-add").append('<input type="hidden" name="cid" value='+ids[i]+'>');
					}
					$("#subreimburse-add").parent().parent().parent().before(subreimburseHtml(employeeList));
					$(".deleteitem-btn").hide();
					form.render();

				});
			});

			//提交表单
			form.on("submit(submit)", function(data) {
				loadAjax( url, method, $("#form").serialize(), function(response) {
					baseCallBack(response, parentReloadCallBack);
				});
				return false;
			})
		});
	</script>
</body>
</html>